package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 查询账户信息（单次）
 * @author F
 *
 */
public class CMB01Deposit extends CMB01Base implements IDeposit{
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
        	
        	CMB01Header bankHeader = new CMB01Header();
            bankHeader.FUNNAM = "GetAccInfo";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("SDKACINFX");
            String bankarea = getBankConfig().getMapValueByKey("bankArea", p_req7507.actArea);
            if ("308584001024".equals(p_req7507.actArea)) {
                bankarea = "1";
            }
            xmlWriter.writeElementString("BBKNBR", 4 == p_req7507.actArea.length() ? bankarea : p_req7507.actArea);
            //ACCNBR 帐号 C(35) 否
            xmlWriter.writeElementString("ACCNBR", p_req7507.actNum);
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
           
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            //模拟返回
            //WriteBankLogStep2(xmlWriter.toXmlString());
            //String[] tmpStrRet=new String[2];
            //tmpStrRet[0]="";
            //tmpStrRet[1]=FileHelper.readFileText("D:\\zjbr-开发便捷小工具类\\xmlDealWith\\cgn02Test.txt", "utf-8");
            //WriteBankLogStep3(tmpStrRet[1]);

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String retCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if ("0".equals(retCode)) {//返回0表示查询成功，可以继续后续数据处理                    
                    	while (tmpBkReader.read("/CMBSDKPGK/NTQACINFZ")){
                            XElement readXElement=tmpBkReader.readedXElement;
                            RetAts7507Detail retAts7507Detail = new RetAts7507Detail();
                            retAts7507Detail.actNum=p_req7507.actNum;//银行账号
                            retAts7507Detail.actArea=p_req7507.actArea;//账号地区区域码
                            retAts7507Detail.depositCode=readXElement.getSingleTextNotNull("ACCNBR").trim();//定存单号
                            retAts7507Detail.startDate=readXElement.getSingleTextNotNull("OPNDAT").trim();//起始日期
                            retAts7507Detail.endDate=readXElement.getSingleTextNotNull("MUTDAT").trim();//截止日期
                            retAts7507Detail.valueDate=readXElement.getSingleTextNotNull("OPNDAT").trim();//起息日期即起始日期
                            //getBankConfig().getMapValueByKey("termMapping",terms);//存款期限 (一天'0,七天`1,一个月`2,三个月`3,六个月`4,一年`5,二年`6,三年`7,五年`8,其它`9)
                            String terms=readXElement.getSingleTextNotNull("DPSTXT").trim();//存期
                            if("一天".equals(terms)){
                                retAts7507Detail.terms="0";
                            }else if("七天".equals(terms)){
                                retAts7507Detail.terms="1";
                            }else if("一个月".equals(terms)){
                                retAts7507Detail.terms="2";
                            }else if("三个月".equals(terms)){
                                retAts7507Detail.terms="3";
                            }else if("六个月".equals(terms)){
                                retAts7507Detail.terms="4";
                            }else if("一年".equals(terms)){
                                retAts7507Detail.terms="5";
                            }else if("二年".equals(terms)){
                                retAts7507Detail.terms="6";
                            }else if("三年".equals(terms)){
                                retAts7507Detail.terms="7";
                            }else if("五年".equals(terms)){
                                retAts7507Detail.terms="8";
                            }else {
                                retAts7507Detail.terms="9";
                            }
                            //retAts7507Detail.terms=readXElement.getSingleTextNotNull("DPSTXT").trim();//存期
                            retAts7507Detail.currencyID=convertCurToATS(readXElement.getSingleTextNotNull("CCYNBR"));//币种
                            retAts7507Detail.noteMoney=readXElement.getSingleTextNotNull("AVLBLV").trim();//金额
                            retAts7507Detail.rate=readXElement.getSingleTextNotNull("INTRAT").trim().replace("%","");//利率
                            String depositStates=readXElement.getSingleTextNotNull("STSCOD").trim();
                            if("A".equals(depositStates)){
                                retAts7507Detail.depositStates="1";
                            }else if("B".equals(depositStates)){
                                retAts7507Detail.depositStates="2";
                            }else {
                                retAts7507Detail.depositStates="";
                            }
                            //retAts7507Detail.depositStates=readXElement.getSingleTextNotNull("STSCOD").trim();//账户状态A=活动，B=冻结，C=关户'
                            retAts7507Detail.drawDate="";//上划日期
                            retAts.writeRetDetail(retAts7507Detail);
                        }    
                     }
                  }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("查无存单");
        }
        retAts.writeEnd();
        return retAts;
    }
}
